package com.ztgf.upload.api.cms;

import com.ztgf.common.enums.FileErrorCode;
import com.ztgf.common.utils.FileCheck;

import com.ztgf.upload.models.service.UploadFileService;
import com.ztgf.upload.models.vo.FastDFSExceptionVO;
import com.ztgf.upload.models.vo.FileResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/cms/upload")
@Api(value = "UploadController",tags = "文件上传下载接口类")
public class UploadController {
    @DubboReference
    private UploadFileService uploadFileService;

    /**
     * 只能上传图片，只上传文件到服务器，不会保存记录到数据库. <br>
     * 会检查文件格式是否正确，默认只能上传 ['png', 'gif', 'jpeg', 'jpg'] 几种类型.
     *
     * @param file
     * @return 返回文件路径等信息
     */
    @ApiOperation(value = "上传图片",notes = "上传图片")
    @PostMapping("/image")
    public FileResponseVO uploadImageSample(@RequestParam("file") MultipartFile file) {
        System.out.println(file.getOriginalFilename());
        // 检查文件类型
        if (!FileCheck.checkImage(file.getOriginalFilename())) {
            FileResponseVO responseData = new FileResponseVO(false);
            responseData.setCode(FileErrorCode.FILE_TYPE_ERROR_IMAGE.CODE);
            responseData.setMessage(FileErrorCode.FILE_TYPE_ERROR_IMAGE.MESSAGE);
            return responseData;
        }

        return uploadSample(file);
    }

    /**
     * 只能上传文档，只上传文件到服务器，不会保存记录到数据库. <br>
     * 会检查文件格式是否正确，默认只能上传 ['pdf', 'ppt', 'xls', 'xlsx', 'pptx', 'doc', 'docx'] 几种类型.
     *
     * @param file
     * @return 返回文件路径等信息
     */
    @ApiOperation(value = "上传文件",notes = "上传文件")
    @PostMapping("/doc")
    public FileResponseVO uploadDocSample(@RequestParam MultipartFile file) {
        // 检查文件类型
        if (!FileCheck.checkDoc(file.getOriginalFilename())) {
            FileResponseVO responseData = new FileResponseVO(false);
            responseData.setCode(FileErrorCode.FILE_TYPE_ERROR_DOC.CODE);
            responseData.setMessage(FileErrorCode.FILE_TYPE_ERROR_DOC.MESSAGE);
            return responseData;
        }

        return uploadSample(file);
    }

    /**
     * 以附件形式下载文件
     *
     * @param filePath 文件地址
     * @param response
     */
    @ApiOperation(value = "以附件形式下载文件",notes = "以附件形式下载文件")
    @RequestMapping("/download")
    public void downloadFile(String filePath, HttpServletResponse response) throws FastDFSExceptionVO {
        try {
            uploadFileService.downloadFile(filePath, response);
        } catch (FastDFSExceptionVO e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 上传通用方法，只上传到服务器，不保存记录到数据库
     *
     * @param file
     * @return
     */
    public FileResponseVO uploadSample(MultipartFile file) {

        FileResponseVO responseData = new FileResponseVO();
        try {
            // 上传到服务器
            String filepath = uploadFileService.uploadFileWithMultipart(file);

            responseData.setFileName(file.getOriginalFilename());
            responseData.setFilePath(filepath);
            responseData.setFileType(FileCheck.getFilenameSuffix(file.getOriginalFilename()));
        } catch (FastDFSExceptionVO e) {
            responseData.setSuccess(false);
            responseData.setCode(e.getCode());
            responseData.setMessage(e.getMessage());
        }

        return responseData;
    }
}
